import service from '../../services';

const state = {
  direction: {
    template: [],
    data: []
  },
  radialStackbarData: null,
  compositionRightData: null
}

const getters = {
  getTemplate (state) {
    return state.direction.template
  },
  getIndustries (state) {
    return state.direction.data
  },
  getRadialStackbar (state) {
    return state.radialStackbarData
  },
  getCompositionRight (state) {
    return state.compositionRightData
  }
}

// 每一个异步操作所需要访问的对应数据都封装于service中
// 这样做的好处是能够清晰地知道发送了什么样的请求，统一管理

const actions = {
  async loadDirectionTemplateData (context, payload) {

    let response = null

    try {
      response = await service.getDirectionTemplateData().catch(err => console.log(err))
    } catch (error) {
      console.log(error)
    }

    // localStorage.setItem('template', JSON.stringify())

    if (response != null && response.length !== 0) {
      context.commit('loadTemplate', response)
    }
  },
  async loadDirectionIndustriesData (context, payload) {
    let response = null;
    try {
      response = await service.getDirectionIndustriesData()
    } catch (error) {
      console.log(error)
    }

    if (response != null) {
      context.commit('loadIndustries', response['target_num'])
      context.commit('loadCompositionRight', response['data_cmb'])
    }
  },
  async loadRadialStackbarData (context, payload) {
    let response = await service.getRadialStackbarData()
    if (response.length !== 0) {
      context.commit('loadRadialStackbar', response)
    }
  },
  async loadCompositionRightData (context, payload) {
    let response = await service.getCompositionRightData()
    if (response.length !== 0) {
      context.commit('loadCompositionRight', response)
    }
  }
}

const mutations = {
  loadTemplate (state, payload) {
    state.direction.template = payload
  },
  loadIndustries (state, payload) {
    state.direction.data = payload
  },
  loadRadialStackbar (state, payload) {
    state.radialStackbarData = payload
  },
  loadCompositionRight (state, payload) {
    state.compositionRightData = payload
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}